<?php

namespace app\admin\controller\mall\goods;

use app\common\controller\Backend;

/**
 * 规格项
 *
 * @icon fa fa-circle-o
 */
class Specitem extends Backend
{
    
    /**
     * Specitem模型对象
     * @var \app\common\model\mall\goods\Specitem
     */
    protected $model = null;
    protected $goods_id = null;
    protected $cat_ids = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\goods\Specitem;
        $this->goods_id = $this->request->param('goods_id');
        $this->assignconfig('goods_id', $this->goods_id);
        //$this->assign("goods_id", $this->goods_id);
        //获取商品的分类IDS
        $this->cat_ids = $this->request->param('cat_ids');
        $this->assignconfig('cat_ids', $this->cat_ids);
        //$this->assign("cat_ids", $this->cat_ids);

    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        $total = db('mall_goods_catspec')
            ->where(["cat_id"=>["in", $this->cat_ids],"showis"=>1])
            ->count();
        if($total==0){
            $this->error("商品所属分类没有相应规格，无法设置规格项！");
        }

        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                    ->alias("si")
                    ->join('__MALL_GOODS_CATSPEC__ cs', "si.spec_id=cs.id","RIGHT")
                    ->where(["cs.cat_id"=>["in", $this->cat_ids],"cs.showis"=>1])
                    ->where($where)
                    ->count();

            $list = $this->model
                    ->alias("si")
                    ->join('__MALL_GOODS_CATSPEC__ cs', "si.spec_id=cs.id","RIGHT")
                    ->where(["cs.cat_id"=>["in", $this->cat_ids],"cs.showis"=>1])
                    ->where($where)
                    ->field("cs.id,cs.specname,cs.allowimgis,si.*")
                    ->order("spec_id,itemid")
                    ->order($sort, $order)
                    ->select();
            $list = collection($list)->toArray();

            //添加未设置的分类规格行
            $specs = db('mall_goods_catspec')
                ->where(["cat_id"=>["in", $this->cat_ids],"showis"=>1])
                ->select();
            $specIds = array_column($list,'spec_id');//已设置的规格ID
            $specIds = array_unique($specIds);
            foreach ($specs as $spec) {
                //未设置的规格ID
                if( !in_array($spec["id"],$specIds) ){
                    $tempRow = [];
                    $tempRow['goods_id'] = $this->goods_id;
                    $tempRow['itemname']= "此规格未设置，请根据需要添加！";
                    $tempRow['spec_id'] = $spec['id'];
                    $tempRow['goods_id_text'] = $this->model->getGoodsIdTextAttr($this->goods_id,[]);
                    $tempRow['spec_id_text'] = $this->model->getSpecIdTextAttr($spec['id'],[]);
                    $tempRow['allowimgis'] = $spec['allowimgis'];
                    $list[] = $tempRow;
                    $total++;
                }
            }

            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        $spec_id = $this->request->param('spec_id');
        $this->assign("spec_id", $spec_id);
        $allowimgis = $this->request->param('allowimgis');
        $this->assign("allowimgis", $allowimgis);
        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $specid = $this->model->get($ids)->spec_id;
        $allowimgis = db("mall_goods_catspec")->where("id",$specid)->column("allowimgis")[0];
        $this->view->assign("allowimgis", $allowimgis);
        return parent::edit($ids);
    }

    public function selectpage()
    {
        return parent::selectpage();
    }
}
